/**
 * This header delares common line storage data structure 
 * for tagfolder
 * First created by chuanshenglu@gmail.com
 */

#if !defined( __TF_LIST_H__)
#define __TF_LIST_H__

#include <stdlib.h>

#if defined(__cplusplus)
extern "C"{
#endif

#if defined( DEFAULT_LIST_SIZE)
#undef DEFAULT_LIST_SIZE
#endif
#define DEFAULT_LIST_SIZE   16u

// Linear sequencial list;
// Stores void*;
typedef struct list_entry{
    void* data;
    size_t size;
}list_entry;
typedef struct array_list{
    struct list_entry* entries;
    size_t length;
}array_list;

int     free_array_list ( array_list* lst);
void*   get_from_array_list ( array_list* lst, size_t index);
int     set_array_list ( array_list* lst, size_t idx, const void* data, size_t sz);
int     push_back_array_list ( array_list* lst, const void* data, size_t sz);
int     push_head_array_list ( array_list* lst, const void* data, size_t sz);
typedef int (*array_list_foreach_callback)( void *);
int     foreach_array_list( array_list* lst, array_list_foreach_callback);
array_list* alloc_array_list ( size_t len=0 );
list_entry* get_entry_of_array_list( array_list* lst, size_t index);
array_list* concact_array_list( array_list* lst1, array_list* lst2);
array_list* copy_array_list( array_list* lst);

/**
 * String list
 */
typedef array_list string_list;

int     free_string_list( array_list* lst);
const char* get_from_string_list( string_list* lst, size_t index);
int     set_string_list( string_list* lst, size_t index, const char* str);
int     push_back_string_list( string_list* lst, const char* str);
int     push_head_string_list( string_list* lst, const char* str);
int     string_list_tok( const char* str, string_list** lst, const char* tok);
string_list* concact_string_list( string_list* lst1, string_list* lst2);
string_list* alloc_string_list( size_t len=0);
string_list* copy_string_list( string_list* lst);


#if defined( FOREACH)
#undef FOREACH
#endif
#define FOREACH( entry, list )   int __idx=0; \
                                 struct list_entry* entry=list->entries[0]; \
                                 for( __idx=0; __idx< list->length; entry=list->entries[__idx], ++__idx) 

#if defined( __cplusplus)
}   //extern "C"{
#endif

#endif // #define __TF_LIST_H__
